﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SponsorshipManager.Domain
{
    public class Relationship
    {
        public enum PaymentFrequency 
        { 
            Monthly = 12 , 
            Trimestral = 4 , 
            Seimestral = 2, 
            Annual = 1
        }
        public List<Quota> Quotas 
        { 
            get; 
            set; 
        }
        public PaymentFrequency Frequency 
        { 
            get; 
            set; 
        }
        public DateTime StartDate
        {
            get;
            set;
        }
        public bool AutoRenewQuotas
        {
            get;
            set;
        }
        public DateTime EndDate
        {
            get;
            set;
        }

        internal Sponsor Sponsor
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        internal Child Child
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public bool RequiresRenewal(int RemainingPayments)
        {
            // If end date is set, and is already passed, no more quotas required.
            if(EndDate>DateTime.MinValue && EndDate < DateTime.Today)
            {
                return false;
            }
            else
            {
                int p = Quotas.Count(x => x.DueDate > DateTime.Today);
                return (p >= RemainingPayments);
            }
        }



        public void AddQuotas(DateTime FirstDueDate, PaymentFrequency RenewalFrequency,
                                int NumberOfQuotasToAdd, decimal NewValue)
        {
            DateTime d = FirstDueDate;
            int m = (int)12/(int)RenewalFrequency;
            for (int n = 0; n < NumberOfQuotasToAdd; n++)
            {
                Quota q = new Quota()
                {
                    DueDate = d,
                    Value = NewValue
                };
                Quotas.Add(q);
                d = d.AddMonths(m);
            }
        }

    }
}
